//
//  HLDownLoadedCell.m
//  HLAerospace
//
//  Created by hulian-ios on 17/9/16.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//


#import "HLDownLoadedCell.h"
#import <Masonry/Masonry.h>
#import "HLCourseComment.h"
#import "HLCourseDetail.h"

@interface HLDownLoadedCell()

/** authorL  */
@property (nonatomic ,strong) UILabel *title;
/** contentNum  */
@property (nonatomic ,strong) UILabel *contentNum;
/** contentL  */
@property (nonatomic ,strong) UIButton *deleteB;
/** timeL  */
@property (nonatomic ,strong) UIButton *editB;


@end

@implementation HLDownLoadedCell


- (UILabel *)title
{
    if (!_title ) {
        _title = [[UILabel alloc] init];
        _title.numberOfLines = 0;
        _title.text = @"航天航空知识简介";
        _title.textColor = HLRGB(60, 60, 60);
        _title.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 14:(kMainTitleFontSize + 2)];
    }
    return _title;
}

- (UILabel *)contentNum
{
    if (!_contentNum ) {
        _contentNum = [[UILabel alloc] init];
        _contentNum.numberOfLines = 0;
        _contentNum.text = @"0M";
        _contentNum.textColor = HLRGB(120, 120, 120);
        _contentNum.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 12:kMainTitleFontSize];
    }
    return _contentNum;
}


- (UIButton *)deleteB
{
    if (!_deleteB ) {
        _deleteB = [[UIButton alloc]init];
        [_deleteB setImage:[UIImage imageNamed:@"mine_course_delete"] forState:UIControlStateNormal];
        [_deleteB addTarget:self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteB;
}

- (UIButton *)editB
{
    if (!_editB ) {
        _editB = [[UIButton alloc] init];
        _editB.titleLabel.font = [UIFont systemFontOfSize:14];
        _editB.backgroundColor = kColorMineBlueBG;
        _editB.layer.cornerRadius = 3;
        _editB.clipsToBounds = YES;
        [_editB setTitle:@"播放" forState:UIControlStateNormal];
        [_editB setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_editB addTarget:self action:@selector(playButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editB;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUI];
    }
    return self;
}

- (void)setUI
{
    [self.contentView addSubview:self.title];
    [self.contentView addSubview:self.deleteB];
    [self.contentView addSubview:self.editB];
    [self.contentView addSubview:self.contentNum];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self configsCons];
}

- (void)configsCons
{
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.centerY.equalTo(self.contentView);
        make.width.equalTo(@(self.width *0.5));
        make.height.equalTo(@(30));
    }];
    
    [self.editB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-15);
        make.centerY.equalTo(self.contentView);
        make.width.equalTo(@(40));
        make.height.equalTo(@(25));
    }];
    
    [self.deleteB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.editB.mas_left).offset(-5);
        make.centerY.equalTo(self.contentView);
        make.width.equalTo(@(30));
        make.height.equalTo(@(30));
    }];
    
    [self.contentNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.deleteB.mas_left).offset(0);
        make.centerY.equalTo(self.contentView);
        make.width.equalTo(@(70));
        make.height.equalTo(@(30));
    }];
}


- (void)setCourseDetail:(HLCourseDetail *)courseDetail
{
    NSLog(@"%@",courseDetail.filePath);
    _courseDetail = courseDetail;
    self.title.text = courseDetail.name;
    self.contentNum.text = [self fileSize: courseDetail.totalSize];
    [_editB setTitle:[courseDetail.favourate_state integerValue] == 2 ? @"查看":@"播放" forState:UIControlStateNormal];
}

- (NSString *)fileSize:(long long )fileSize
{
    /*** 计算缓存大小 ***/
    NSInteger size = fileSize;
    CGFloat unit = 1000.0;
    NSString *sizeText = nil;
    if (size >= unit * unit * unit) { // >= 1GB
        sizeText = [NSString stringWithFormat:@"%.1fGB", size / unit / unit / unit];
    } else if (size >= unit * unit) { // >= 1MB
        sizeText = [NSString stringWithFormat:@"%.1fMB", size / unit / unit];
    } else if (size >= unit) { // >= 1KB
        sizeText = [NSString stringWithFormat:@"%.1fKB", size / unit];
    } else { // >= 0B
        sizeText = [NSString stringWithFormat:@"%zdB", size];
    }
    return sizeText;
}


- (void)deleteButtonClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(downLoadedCell:didClickDeleteCourseDetail:)]) {
        [self.delegate downLoadedCell:self didClickDeleteCourseDetail:self.courseDetail];
    }
}

- (void)playButtonClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(downLoadedCell:didClickPlayCourseDetail:)]) {
        [self.delegate downLoadedCell:self didClickPlayCourseDetail:self.courseDetail];
    }
}

- (void)setFrame:(CGRect)frame
{
    frame.origin.y += 1;
    frame.size.height = frame.size.height - 1;
    [super setFrame:frame];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

@end
